package com.dz.scrm.utils;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dz.scrm.R;


public class ToastUtils {

    /**
     * 之前显示的内容
     */
    private static String oldMsg;
    /**
     * Toast对象
     */
    private static Toast toast = null, layoutToast = null;
    /**
     * 第一次时间
     */
    private static long oneTime = 0;
    /**
     * 第二次时间
     */
    private static long twoTime = 0;

    /**
     * 显示Toast,防止Toast重复提示
     *
     * @param context
     * @param message
     */
    public static void showToast(Context context, String message) {
        if (!TextUtils.isEmpty(message)) {
            return;
        }
        try {
            if (toast == null) {
                toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
			    toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
                oneTime = System.currentTimeMillis();
            } else {
                twoTime = System.currentTimeMillis();
                if (toast == null) {
                    toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
                }
                if (message != null && message.equals(oldMsg)) {
                    if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                        toast.show();
                    }
                } else {
                    oldMsg = message;
                    toast.setText(message);
                    toast.show();
                }
            }
            oneTime = twoTime;
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 显示Toast,防止Toast重复提示
     *
     * @param context
     * @param message
     */
    public static void showToastMsg(Context context, String message) {
        try {
            if (toast == null) {
                toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
                oneTime = System.currentTimeMillis();
            } else {
                twoTime = System.currentTimeMillis();
                if (toast == null) {
                    toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
                }
                if (message.equals(oldMsg)) {
                    if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                        toast.show();
                    }
                } else {
                    oldMsg = message;
                    toast.setText(message);
                    toast.show();
                }
            }
            oneTime = twoTime;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     *
     * @param context
     * @param message
     * @param layoutID 布局文件id
     * 显示Toast,防止Toast重复提示
     */
    public static void ToastMsg(Context context, String message, int layoutID) {
        try {
            LinearLayout toastView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.view_toast2, null);
            TextView textView = (TextView) toastView.findViewById(R.id.toast_text);
            textView.setText(message);
            toastView.setBackgroundResource(R.drawable.shape_toast_layout_bg1);
            if (layoutToast == null) {
                layoutToast = new Toast(context);
                layoutToast.setGravity(Gravity.CENTER, 0, 0);
                layoutToast.setDuration(Toast.LENGTH_SHORT);
                layoutToast.setView(toastView);
                layoutToast.show();
                oneTime = System.currentTimeMillis();
            } else {
                twoTime = System.currentTimeMillis();
                if (layoutToast == null) {
                    layoutToast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
                }
                if (message.equals(oldMsg)) {
                    if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                        layoutToast.setGravity(Gravity.CENTER, 0, 0);
                        layoutToast.setDuration(Toast.LENGTH_SHORT);
                        layoutToast.setView(toastView);
                        layoutToast.show();
                    }
                } else {
                    oldMsg = message;
                    layoutToast.setGravity(Gravity.CENTER, 0, 0);
                    layoutToast.setDuration(Toast.LENGTH_SHORT);
                    layoutToast.setView(toastView);
                    layoutToast.show();
                }
            }
            oneTime = twoTime;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void reset() {
        toast = null;

        layoutToast = null;
    }
}